package com.wsg.lightaccounting.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wsg.lightaccounting.Model.AccountManager;
import com.wsg.lightaccounting.Model.CashCard;
import com.wsg.lightaccounting.Model.Customized;
import com.wsg.lightaccounting.PopupWindow.ColorChoosePopupWindow;
import com.wsg.lightaccounting.PopupWindow.EditPopupWindow;
import com.wsg.lightaccounting.R;

public class CustomzedActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView back,check;
    private LinearLayout balance_input,name_input,color_choose;
    private TextView balance_text,name_text;
    private ImageView color_icon;
    private boolean isEdit = false;
    private long id = -1L;
    public static int color = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customzed);
        Bundle bundle = this.getIntent().getExtras();
        isEdit = bundle.getBoolean("isEdit");
        color = 0;
        intiView();
    }

    private void intiView(){
        back = (ImageView) findViewById(R.id.back);
        check = (ImageView) findViewById(R.id.check);

        balance_input = (LinearLayout) findViewById(R.id.balance_input);
        color_choose = (LinearLayout) findViewById(R.id.color_choose);
        name_input = (LinearLayout) findViewById(R.id.name_input);

        balance_text = (TextView) findViewById(R.id.balance_text);
        color_icon = (ImageView) findViewById(R.id.color_icon);
        name_text = (TextView) findViewById(R.id.name_text);

        back.setOnClickListener(this);
        check.setOnClickListener(this);

        balance_input.setOnClickListener(this);
        color_choose.setOnClickListener(this);
        name_input.setOnClickListener(this);

        if (isEdit){
            Bundle bundle = this.getIntent().getExtras();
            name_text.setText(bundle.getString("name"));
            balance_text.setText(String.valueOf(bundle.getFloat("balance")));
            color = bundle.getInt("color");
            AccountManager.setAccountColor(this,color_icon,color);
            id = bundle.getLong("id");
            TextView textView = (TextView) findViewById(R.id.title);
            textView.setText("编辑自定义");
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.check:
                enter();
                MainActivity.updateAllUI(this);
                finish();
                break;
            case R.id.balance_input:
                showEditPopupWinddow(balance_text, EditPopupWindow.INPUT_LIMIT_FLAOT);
                break;
            case R.id.color_choose:
                showChooseColorsWindow();
                break;
            case R.id.name_input:
                showEditPopupWinddow(name_text,EditPopupWindow.INPUT_LIMIT_NONE);
                break;
            default:
                break;
        }
    }
    private void showEditPopupWinddow(TextView textView,int i){
        EditPopupWindow editPopupWindow = new EditPopupWindow(this,this.findViewById(R.id.mark_size).getWidth(),
                this.findViewById(R.id.mark_size).getHeight(),textView,i);
        editPopupWindow.showAtLocation(this.findViewById(R.id.mark_size), Gravity.BOTTOM,0,0);
    }
    private void showChooseColorsWindow(){
        ColorChoosePopupWindow choosePopupWindow = new ColorChoosePopupWindow(this,this.findViewById(R.id.mark_size).getWidth(),
                this.findViewById(R.id.mark_size).getHeight()*2,color_icon);
        choosePopupWindow.showAtLocation(this.findViewById(R.id.mark_size), Gravity.BOTTOM,0,0);
    }

    private void enter(){
        String name = String.valueOf(name_text.getText());
        String balance_text = String.valueOf(this.balance_text.getText());
        float balance = 0f;
        if (!balance_text.equals("")){
            balance = Float.parseFloat(balance_text);
        }
        if (!isEdit){
            AccountManager.getAccountManager(this).addCustomezed(new Customized(0L,name,0L),balance,color);
        }
        else{
            AccountManager.getAccountManager(this).updateCustomzed(id,name,balance,color);
        }
    }
}
